package PTA.javajibenyufa;

import java.util.Arrays;
import java.util.Scanner;

public class ex03 {
    public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
String people[] = new String[n];//存身份证
String birth[] = new String[n];//存生日
        String line;
for(int i=0;i<n;i++){
    people[i]=sc.next();//把身份证存入数组中
}
while(true){
    line = sc.next();
    if(line.equals("sort1")){
        for(int i=0;i<n;i++){
            birth[i]=people[i].substring(6,10)+"-"+people[i].substring(10,12)+"-"+people[i].substring(12,14);

        }
       Arrays.sort(birth);//对生日进行升序排序
       for(int i=0;i<n;i++){
           System.out.println(birth[i]);
       }
    }else if(line.equals("sort2")){
        for(int i=0;i<n;i++){
            birth[i]=people[i].substring(6,10)+people[i].substring(10,12)+people[i].substring(12,14);

        }
        Arrays.sort(birth);//对生日进行升序排序
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(people[j].contains(birth[i])){
                    System.out.println(people[j]);
                    break;
                }
            }
        }
    }else if(line.equals("e")){
        System.out.println("exit");
        break;
    }
}







    }
}
